package com.ruixi.controller;

import com.google.common.collect.Maps;
import com.ruixi.entity.User;
import com.ruixi.service.IUserService;
import com.ruixi.utils.PageBean;
import com.ruixi.utils.RT;
import com.ruixi.utils.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lrzg
 * @date 2021-10-18 11:17
 * @Description TODO
 */
@Api(tags = "用户系统")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    //http://localhost:8080/user/findAllUser
    @ApiOperation(value = "查询全部用户", notes = "用户系统，查询全部用户")
    @GetMapping("findAllUser")
    public List<User> findAllUser() {
        return userService.findAllUser();
    }

    //localhost:8080/user/user/getUser/41
    @ApiOperation(value = "根据ID查询用户", notes = "用户系统，根据ID查询用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Long", defaultValue = "41", paramType = "query", example = "41", required = true)
    )
    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Long id) {
        return userService.getById(id);
    }

    //http://localhost:8080/user/getUserByName?name=%E4%BA%A8%E6%B0%8F，返回单个
    @ApiOperation(value = "根据用户名查询单个用户", notes = "用户系统，根据用户名查询单个用户")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", defaultValue = "亨氏", paramType = "query", example = "亨氏", required = true)
    )
    @GetMapping("/getUserByName")
    public RT<User> getUserByName(@RequestParam("name") String name) {
        User userByName = userService.getUserByName(name);
        return RT.success(userByName);
    }

    @ApiOperation(value = "根据用户名查询多个用户", notes = "用户系统，根据用户名查询多个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", defaultValue = "老王", paramType = "query", example = "亨氏", required = true)}
    )
    @GetMapping("/getUserListByName")
    public ResultInfo getUserListByName(@RequestParam("name") String name) {
        List<User> list = userService.getUserListByName(name);
        return new ResultInfo().result(list);
    }

    @ApiOperation(value = "分页查询", notes = "用户系统，分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", defaultValue = "1", paramType = "query", example = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", defaultValue = "3", paramType = "query", example = "3", required = true)}
    )
    @GetMapping(value = "/getUserListPage")
    @ResponseBody
    public RT<PageBean<User>> getUserListPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        PageBean pageBean = new PageBean(pageNum, pageSize);
        PageBean<User> stuPageInfo = userService.getUserListPage(pageBean);
        RT result = RT.success(stuPageInfo);
        return result;
    }

    @ApiOperation(value = "添加用户", notes = "用户系统，添加用户")
    @PostMapping("addUser")
    public RT<User> addUser(@RequestBody User user) {
        int userId = userService.addUser(user);
        return RT.success(userId);
    }

    @ApiOperation(value = "插入用户", notes = "用户系统，插入用户")
    @PostMapping("/saveUser")
    public RT<Integer> saveUser(@RequestBody User user) {
        int result = userService.saveUser(user);
        return RT.success(result);
    }

    @ApiOperation(value = "根据ID删除用户", notes = "用户系统，根据ID删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户名ID", dataType = "Long", defaultValue = "41", paramType = "query", example = "41", required = true)}
    )
    @GetMapping("/deleteUserById")
    public RT<Integer> deleteUserById(@RequestParam("id") Long id) {
        int result = userService.deleteUserById(id);
        return RT.success(result);
    }

    @ApiOperation(value = "根据用户名更新用户", notes = "用户系统，根据用户名更新用户")
    @PostMapping("/updateUserByName")
    public RT<User> updateUserByName(@RequestBody User user) {
        User userInfo = userService.updateUserByName(user);
        return RT.success(userInfo);
    }

    @ApiOperation(value = "根据用户id更新用户", notes = "用户系统，根据用户id更新用户")
    @PostMapping("/updateUserById")
    public RT<User> updateUserById(@RequestBody User user) {
        User userInfo = userService.updateUserById(user);
        return RT.success(userInfo);
    }

    @ApiOperation(value = "更新用户", notes = "用户系统，更新用户")
    @PostMapping("/updateUser")
    public RT<Integer> updateUser(@RequestBody User user) {
        int result = userService.updateUser(user);
        return RT.success(result);
    }




    @ApiOperation(value = "RequestBody接收Map参数", notes = "RequestBody接收Map参数")
    @PostMapping(value = "/map")
    public RT<Map<String, Object>> map(@RequestBody Map<String, Object> params) {
        return RT.success(params);
    }

    @ApiOperation(value = "RequestParam接收数组参数", notes = "RequestParam接收数组参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "数组", dataType = "String[]", defaultValue = "1", paramType = "query", example = "1", required = true)}
    )
    @GetMapping("/arr")
    public RT<String[]> arr(@RequestParam("name") String[] names) {
        return RT.success(names);
    }


}
